<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Inventory extends MY_Controller {

	public $data 	= 	array();
 	public $_template = array();
	public function __construct() {
		//parent::Controller();
		parent::__construct(); //for CI 2.x users 
		$this->load->module_model('member', 'inventory_model', 'inventory');
		$this->load->helper('text');
		$this->load->module_language('member', 'product');
		$this->user = $this->access->get_user();
		$this->load->helper('url');
		$this->access->restrict();
	}
	
 	/*
 	 *  Hiển thị danh sách các trang thiết bị có trong kho. Gồm cả những thiết bị mới nhập chưa duyệt
 	 */
	public function index() 
	{
		$data['ItemID'] = 'Inventory';
		$data['title'] = 'Danh sách thiết bị và vật liệu';
		$data['description'] = 'Quản lý thiết bị và vật liệu trong kho';
		$this->access->restrict();
		$this->load->library('basic_library');		
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = base_url().'member/inventory/';
		$config['total_rows'] = $this->inventory->count_all_items();
		//echo $config['total_rows'];
		$config['per_page'] = 30;
		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(3))? $this->uri->segment(3):0;
		$data['myproducts'] = $this->inventory->get_items($this->user->id,$config['per_page'], $page);
		$data['links'] = $this->pagination->create_links();
		//$data['products'] = $this->store->get_product_of_category($id,$config['per_page'], $page);
		
		$this->_template['page']	= 'users/inventory/show';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function additem()
	{
		$data['ItemID'] = 'AddItem';
		$data['title'] = 'Thêm mới trang thiết bị quản lý tới kho';
		$data['description'] = 'Quản lý trang thiết bị';
		//use for multi upload icon, thumbs
		//$this->load->library('form_validation');
		//$this->load->library('upload');
		//$this->load->library('validation');
		$this->load->helper('form');
		//$config['upload_path']   = './uploads/products/images'; //if the files does not exist it'll be created
		//$config['allowed_types'] = 'gif|jpg|png';
		//$config['max_size']   = '4000'; //size in kilobytes
		//$config['encrypt_name']  = TRUE;
		//$this->upload->initialize($config);
		//$uploaded = $this->upload->up(); //Pass true if you want to create the index.php files
		
		//var_dump($uploaded['success']);
		//var_dump($uploaded);
		//var_dump($uploaded);
		//foreach($uploaded['success'] as $file){
		//	echo $file['file_name'];
		//}
		
		//$file1['file_name']
		//endforeach;
		
		//$data['categories'] = $this->products->get_all_category();
		//$data['apps'] = $this->products->get_all_app();
		$userid = $this->user->id;
		$lang_working = load_language_working();
		
		//here is validation form
		$this->form_validation->set_rules('productname', 'lang:form_product_name', 'required|max_length[50]|xss_clean|callback_check_productname');
		$this->form_validation->set_rules('alias', 'lang:form_alias', 'callback_check_alias');
		$this->form_validation->set_rules('price', 'lang:form_price', 'required|xss_clean|trim|numeric');
		$this->form_validation->set_rules('app','lang:form_application','required|numeric');
		$this->form_validation->set_rules('category','lang:form_category','required|numeric');
		$this->form_validation->set_rules('description','lang:form_descrtiption','required|max_length[255]');
		/*if($_FILES['file_1']=='' || empty($_FILES['file_2']))
		{
			$this->form_validation->set_rules('file_1','lang:form_thumbs1','required');
		}*/

		$this->form_validation->set_error_delimiters('', '<br />');

		if ($this->form_validation->run() == TRUE)
		{		
			$this->products->add_product($userid, $uploaded['success']);
			$this->session->set_flashdata('message', lang('successfully_created'));	
			redirect('member/inventory/', 'refresh');
		}	
		$this->_template['page']	= 'users/inventory/additem';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function showphieunhap()
	{
        
    }
    public function addphieunhap1()
    {
    	
    }
	public function addphieunhap2()
    {
    	
    }
	//Edit product
	public function edititem($id)
	{
		//metadata
		$data['ItemID']		 = 'Edititem';
		$data['title']		 = "Chỉnh sửa trang thiết bị"; 
		$data['description'] = "Chỉnh sửa trang thiết bị"; 	
		
		$data['item'] 	= $this->inventory->get_the_item($id);
		
		$this->form_validation->set_rules('productname', 'lang:form_product_name', 'required|max_length[50]|xss_clean');
		if($data['product']->alias === strtolower(auto_alias($this->input->post('alias'))) || $this->input->post('alias')=='')
		{
			$this->form_validation->set_rules('alias', 'lang:form_alias', 'max_length[250]');
		}
		else
		{
			$this->form_validation->set_rules('alias', 'lang:form_alias', 'callback_check_alias');
		}
		
		$this->form_validation->set_rules('price', 'lang:form_price', 'required|xss_clean|trim|numeric');
		$this->form_validation->set_rules('app','lang:form_application','required|numeric');
		$this->form_validation->set_rules('category','lang:form_category','required|numeric');
		$this->form_validation->set_rules('description','lang:form_descrtiption','required|max_length[255]');					
		$this->form_validation->set_error_delimiters('', '<br />');
		
		if($this->form_validation->run() == TRUE)
		{
			
			if(!$this->upload->do_upload())
			{
				$this->session->set_flashdata('message', $this->upload->display_errors());
				redirect($this->uri->uri_string());
			}
			else
			{
				$this->products->edit_product($id);
				$this->session->set_flashdata('message', lang('successfully_edited'));	
				redirect('member/inventory/', 'refresh');
			}
		}	
		$this->_template['page']	= 'users/iventory/edit';
		$this->system_library->load($this->_template['page'], $data);
	}
	/*
	 * Xác nhận các thông tin của item là chính xác, sau khi click submit thì ko thể chỉnh sửa item này
	 * set status = 1
	 */
	public function submitinventory()
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}		
		redirect('member/inventory', 'refresh');
		
	}
	public function delete_item()
	{
		$id = $this->input->post('id');
		$json = array();
		if($this->products->delete_thumb_product($id))
		{
			$json['result'] = 1;
		}
		else
		{
			$json['result'] = 0;
		}
		
		echo json_encode($json);
		
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/products.php */